package com.ddh.ddhsanguogame.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddh.ddhsanguogame.dto.CardAllianceDTO;
import com.ddh.ddhsanguogame.entity.CardAlliance;
import com.ddh.ddhsanguogame.entity.CardUserAlliance;
import com.ddh.ddhsanguogame.exception.DongException;
import com.ddh.ddhsanguogame.exception.ErrorCode;
import com.ddh.ddhsanguogame.mapper.CardAllianceMapper;
import com.ddh.ddhsanguogame.mapper.CardUserAllianceMapper;
import com.ddh.ddhsanguogame.service.ICardUserAllianceService;
import com.ddh.ddhsanguogame.service.ICardAllianceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

import static com.ddh.ddhsanguogame.constant.AllianceConstants.ALLIANCE_LEADER;
import static com.ddh.ddhsanguogame.constant.AllianceConstants.ALLIANCE_MEMBER;

@Service
public class CardAllianceServiceImpl extends ServiceImpl<CardAllianceMapper, CardAlliance> implements ICardAllianceService {

    @Resource
    private ICardUserAllianceService cardUserAllianceService;

    /**
     * 创建或加入同盟
     * @param cardAllianceDTO
     * @return
     */
    @Override
    public Boolean createOrJoinAlliance(CardAllianceDTO cardAllianceDTO) {
        // 检查用户是否已经创建过同盟
        LambdaQueryWrapper<CardUserAlliance> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CardUserAlliance::getUserId, cardAllianceDTO.getUserId());
        if (cardUserAllianceService.count(lambdaQueryWrapper) > 0) {
            throw new DongException(ErrorCode.ALLIANCE_NAME_DUPLICATE_ERROR);
        }
        // 检查同盟名称是否已存在
        LambdaQueryWrapper<CardAlliance> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CardAlliance::getName, cardAllianceDTO.getName());
        CardAlliance existingAlliance = this.getOne(queryWrapper);
        if (existingAlliance != null) {
            throw new DongException(ErrorCode.USER_ALREADY_HAS_ALLIANCE);
        }
        // 根据同盟ID是否存在来判断用户是创建还是加入同盟
        if (cardAllianceDTO.getAllianceId() == null) {
            // 创建新的同盟
            CardAlliance cardAlliance = new CardAlliance();
            cardAlliance.setName(cardAllianceDTO.getName());
            cardAlliance.setDescription(cardAllianceDTO.getDescription());
            cardAlliance.setCreatedAt(LocalDateTime.now());
            save(cardAlliance);

            // 创建同盟用户关联信息，创建者为盟主
            CardUserAlliance cardUserAlliance = new CardUserAlliance();
            cardUserAlliance.setUserId(cardAllianceDTO.getUserId());
            cardUserAlliance.setAllianceId(cardAlliance.getId());
            cardUserAlliance.setJoinTime(LocalDateTime.now());
            cardUserAlliance.setPosition(ALLIANCE_LEADER);
            return cardUserAllianceService.save(cardUserAlliance);
        } else {
            // 同盟已存在，用户加入现有同盟
            CardUserAlliance cardUserAlliance = new CardUserAlliance();
            cardUserAlliance.setUserId(cardAllianceDTO.getUserId());
            cardUserAlliance.setAllianceId(cardAllianceDTO.getAllianceId());
            cardUserAlliance.setJoinTime(LocalDateTime.now());
            cardUserAlliance.setPosition(ALLIANCE_MEMBER);
            return cardUserAllianceService.save(cardUserAlliance);
        }
    }
}
